package com.yincb;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import io.netty.handler.stream.ChunkedWriteHandler;

import javax.net.ssl.SSLException;
import java.io.File;
import java.net.URL;
import java.security.cert.CertificateException;

public class WebProxySSLServer {

    private int port;

    public WebProxySSLServer(int port) {
        this.port = port;
    }

    public void start() throws InterruptedException, CertificateException, SSLException {
        final SslContext sslCtx;
        SelfSignedCertificate cert = new SelfSignedCertificate();
        File certificate = cert.certificate();
        File privateKey = cert.privateKey();
        System.out.println(String.format("certificate = %s \r\n privateKey = %s",certificate.toPath(),privateKey.toPath()));

        /*String sslPath = this.getClass().getClassLoader().getResource("ssl").getFile();
        File certificate = new File(sslPath+"/keyutil_example.com_5782695760835501626.crt");
        File privateKey = new File(sslPath + "/keyutil_example.com_3292160782717719987.key");*/

        sslCtx = SslContextBuilder.forServer(certificate, privateKey).build();

        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            // HTTPS
                            ch.pipeline().addLast("https-hander",sslCtx.newHandler(ch.alloc()));
                            // 请求消息解码器
                            ch.pipeline().addLast("http-decoder",new HttpRequestDecoder());
                            // 目的是将多个消息转换为单一的request或者response对象
                            ch.pipeline().addLast("http-aggregator",new HttpObjectAggregator(65536));
                            // 响应解码器
                            ch.pipeline().addLast("http-encoder",new HttpResponseEncoder());
                            // 目的是支持异步大文件传输
                            ch.pipeline().addLast("http-chunked",new ChunkedWriteHandler());
                            ch.pipeline().addLast("httpProxyServerHandler",new HttpsProxyServerHandler());
                        }
                    });

            // Start the server.
            ChannelFuture f = b.bind(port).sync();

            // Wait until the server socket is closed.
            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws InterruptedException, CertificateException, SSLException {
        int port = 443;
        WebProxySSLServer server = new WebProxySSLServer(port);
        server.start();
    }
}




